package com.yy.wga.mp.module.user.controller;

import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.common.aop.NotLogin;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.module.user.manager.UserManager;
import com.yy.wga.mp.module.user.request.ModifyPhoneNumberRequest;
import com.yy.wga.mp.module.user.request.MpLockFansRequest;
import com.yy.wga.mp.module.user.request.SaveUserInfoRequest;
import com.yy.wga.mp.module.user.request.SaveWxNumRequest;
import com.yy.wga.mp.module.user.request.SendModifyPhoneNumberRequest;
import com.yy.wga.mp.module.user.request.UserRegisterAndBindStatusSetRequest;
import com.yy.wga.mp.module.user.request.UserWechatInfoSetRequest;
import com.yy.wga.mp.module.user.request.UserWxNumberSetRequest;
import com.yy.wga.mp.module.user.request.VerifyOldPhoneCodeRequest;
import com.yy.wga.mp.module.user.request.WxAuthCode2SessionRequest;
import com.yy.wga.mp.module.user.request.WxGetPhoneNumberRequest;
import com.yy.wga.mp.module.user.request.WxGetUserInfoRequest;
import com.yy.wga.mp.module.user.response.QueryAccountResponse;
import com.yy.wga.mp.module.user.response.UserParentInfoGetResponse;
import com.yy.wga.mp.module.user.response.UserServerWxNumberGetResponse;
import com.yy.wga.mp.module.user.response.WxAuthCode2SessionResponse;
import com.yy.wga.mp.module.user.response.WxGetPhoneNumberResponse;
import com.yy.wga.mp.module.user.response.WxGetUserInfoResponse;
import com.yy.wga.mp.module.user.service.UserRelationService;
import com.yy.wga.user.feign.api.UserBaseFeign;
import com.yy.wga.user.feign.request.UserBaseSaveRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户管理
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2020/6/5,11:30
 * @since 1.0.0     2020/6/5,11:30
 */
@RestController
@RequestMapping(value = "/api/user/v1/")
public class UserController {

    @Resource
    private UserBaseFeign userBaseFeign;

    @Resource
    private UserRelationService userRelationService;
    @Autowired
    private UserManager userManager;

    /**
     * 小程序微信code登录，没有用户信息
     *
     * @param request
     * @return
     */
    @NotLogin
    @PostMapping("authCode2Session")
    public Result<WxAuthCode2SessionResponse> authCode2Session(@Validated @RequestBody WxAuthCode2SessionRequest request) {
        return Result.success(userManager.authCode2Session(request));
    }

    /**
     * 小程序用户信息授权
     *
     * @param request
     * @return
     */
    @PostMapping("getWxUserInfo")
    public Result<WxGetUserInfoResponse> getWxUserInfo(@Validated @RequestBody WxGetUserInfoRequest request) {
        return Result.success(userManager.getWxUserInfo(request));
    }

    /**
     * 修改用户信息
     *
     * @param request
     * @return
     */
    @PostMapping("saveUserInfo")
    public Result<String> saveUserInfo(@Validated @RequestBody SaveUserInfoRequest request) {
        if (StringUtils.isBlank(request.getAvatar())
                && StringUtils.isBlank(request.getUserName())) {
            return Result.fail("请输入修改的内容");
        }
        UserBaseSaveRequest updateRequest = new UserBaseSaveRequest();
        updateRequest.setUserId(RequestUserHolder.getUserCache().getUserId());
        updateRequest.setNickName(request.getUserName());
        updateRequest.setAvatarUrl(request.getAvatar());
        Result<Long> result = userBaseFeign.save(updateRequest);
        if (!result.isSuccess()) {
            return Result.fail(result.getMessage());
        }
        return Result.success();
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("logout")
    public Result<Boolean> logout() {
        userManager.logout();
        return Result.success(Boolean.TRUE);
    }

    /**
     * 修改手机号-发送原手机号验证码
     *
     * @return
     */
    @PostMapping("modifyPhone/sendVerifyOldPhoneCode")
    public Result<String> sendVerifyOldPhoneCode() {
        userManager.sendVerifyOldPhoneCode();
        return Result.success();
    }

    /**
     * 修改手机号-校验原手机号验证码
     *
     * @param request
     * @return
     */
    @PostMapping("modifyPhone/verifyOldPhoneCode")
    public Result<String> verifyOldPhoneCode(@Validated @RequestBody VerifyOldPhoneCodeRequest request) {
        userManager.verifyOldPhoneCode(request);
        return Result.success();
    }

    /**
     * 发送更换手机号验证码-新原手机号
     *
     * @param request
     * @return
     */
    @PostMapping("sendModifyPhoneCode/newPhone")
    public Result<String> sendModifyPhoneCode(@Validated @RequestBody SendModifyPhoneNumberRequest request) {
        userManager.sendModifyPhoneCode(request);
        return Result.success();
    }

    /**
     * 更换手机号
     *
     * @param request
     * @return
     */
    @PostMapping("modifyPhoneNumber")
    public Result<String> modifyPhoneNumber(@Validated @RequestBody ModifyPhoneNumberRequest request) {
        userManager.modifyPhoneNumber(request);
        return Result.success();
    }

    /**
     * 保存微信号
     *
     * @param request
     * @return
     */
    @PostMapping("saveWxNum")
    public Result<QueryAccountResponse> saveWxNum(@Validated @RequestBody SaveWxNumRequest request) {
        return Result.success(userManager.saveWxNum(request));
    }

    /**
     * 查询用户基本信息
     *
     * @return
     */
    @PostMapping("queryAccount")
    public Result<QueryAccountResponse> queryAccount() {
        return Result.success(BeansUtils.map(RequestUserHolder.getUserCache(), QueryAccountResponse.class));
    }

    /**
     * 授权手机号
     *
     * @param request
     * @return
     */
    @PostMapping("authPhoneNumber")
    public Result<WxGetPhoneNumberResponse> authPhoneNumber(@Validated @RequestBody WxGetPhoneNumberRequest request) {
        return Result.success(userManager.authPhoneNumber(request));
    }

    /**
     * 锁粉
     *
     * @param request
     * @return
     */
    @PostMapping("lockFans")
    public Result<Boolean> authPhoneNumber(@Validated @RequestBody MpLockFansRequest request) {
        if (request.getParentUserId() == null && StringUtils.isBlank(request.getInviteCode())) {
            return Result.fail("参数错误");
        }
        userRelationService.lockFans(request);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 设置微信号
     *
     * @param request
     * @return
     */
    @PostMapping("setWxNum")
    public Result<Boolean> setWxNum(@Validated @RequestBody UserWxNumberSetRequest request) {
        UserBaseSaveRequest updateRequest = new UserBaseSaveRequest();
        updateRequest.setUserId(RequestUserHolder.getUserCache().getUserId());
        updateRequest.setWxNum(request.getWxNum());
        Result<Long> result = userBaseFeign.save(updateRequest);
        if (!result.isSuccess()) {
            return Result.fail(result.getMessage());
        }
        return Result.success(Boolean.TRUE);
    }

    /**
     * 获取服务商微信号
     *
     * @return
     */
    @PostMapping("getServerWxNum")
    public Result<UserServerWxNumberGetResponse> getServerWxNum() {
        UserServerWxNumberGetResponse response = userManager.getServerWxNum();
        return Result.success(response);
    }

    /**
     * 获取上级信息
     *
     * @return
     */
    @PostMapping("getParentInfo")
    public Result<UserParentInfoGetResponse> getParentInfo() {
        UserParentInfoGetResponse response = userManager.getParentInfo();
        return Result.success(response);
    }

    /**
     * 设置获取用户注册并且绑定接口
     *
     * @param request
     * @return
     */
    @PostMapping("setRegisterBindStatus")
    public Result<QueryAccountResponse> setRegisterBindStatus(@Validated @RequestBody UserRegisterAndBindStatusSetRequest request) {
        return Result.success(userManager.setRegisterBindStatus(request));
    }

    /**
     * 设置获取用户微信信息
     *
     * @param request
     * @return
     */
    @PostMapping("setUserWechatInfo")
    public Result<QueryAccountResponse> setUserWechatInfo(@Validated @RequestBody UserWechatInfoSetRequest request) {
        return Result.success(userManager.setUserWechatInfo(request));
    }


}
